package cn.edu.scau.controller;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;


import cn.edu.scau.entity.Blog;
import cn.edu.scau.entity.Comment;
import cn.edu.scau.service.BlogService;
import cn.edu.scau.service.UserService;


@Controller
@RequestMapping("/blog")
public class BlogController {

	@Autowired
	private BlogService blogService;
	@Autowired
	private UserService userService;

	public UserService getUserService() {
		return userService;
	}

	public void setUserService(UserService userService) {
		this.userService = userService;
	}

	public BlogService getBlogService() {
		return blogService;
	}

	public void setBlogService(BlogService blogService) {
		this.blogService = blogService;
	}

	@RequestMapping("/jump")
	public String jump() {
		return "index";
	}

	public String savePeople() {
		return "index";
	}

	/**
	 * 首页blog
	 *
	 * @param 页数
	 * 
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/findBlogs")
	public List<Blog> findBlogs(@RequestParam(name = "pageNo") Integer pageNo) {
		// 返回


		return this.getBlogService().getAllBlog(pageNo);
	}

	/**
	 * 所有博客的最大页数
	 * 
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/findAllBlogMaxPage")
	public String findAllBlogMaxPage() {
		long page = 0;
		try {
			// 获得最大页数
			page = this.getBlogService().allOrSearchMaxpage(0, null);

		} catch (Exception e) {
			return "{" + '"' + "allBlogMaxPage" + '"' + ":" + '"' + 0 + '"' + "}";
		}
		return "{" + '"' + "allBlogMaxPage" + '"' + ":" + '"' + page + '"' + "}";
	}

	/**
	 * 用户评论
	 * 
	 * @param comment
	 * 评论
	 * @param blogId
	 * 博客id
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/comment")
	public String userComment(Comment comment, Integer blogId) {

		try {
			// 设置时间


			comment.setTime(new SimpleDateFormat("yyyy:MM:dd HH:mm:ss").format(new Date().getTime()));
			// 添加评论


			this.getBlogService().comment(comment, blogId);
		} catch (Exception e) {
			e.printStackTrace();
			return "{" + '"' + "result" + '"' + ":" + '"' + false + '"' + "}";
		}

		return "{" + '"' + "result" + '"' + ":" + '"' + true + '"' + "}";
	}

	/**

	 * 获取博客的评论
	 * 
	 * @param blogId
	 * 博客id
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/blogComments")
	public List<Comment> blogComments(@RequestParam(name = "blogId") int blogId) {
		// 返回


		return this.getBlogService().getAllComment(blogId);
	}

	/**

	 * 点赞
	 * 
	 * @param blogId
	 * 博客id
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/userUpvote")
	public String userUpvote(@RequestParam(name = "blogId") int blogId) {

		try {
			this.getBlogService().upvote(blogId);
		} catch (Exception e) {
			return "{" + '"' + "result" + '"' + ":" + '"' + false + '"' + "}";
		}
		return "{" + '"' + "result" + '"' + ":" + '"' + true + '"' + "}";

	}

	/**

	 * 搜索的博客最大页面总数
	 *
	 * @param condition
	 * 搜索条件
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/searchBlogMaxPage")
	public String searchBlogMaxPage(@RequestParam(name = "condition") String condition) {
		long page = 0;
		try {
			// 获得最大页数
			page = this.getBlogService().allOrSearchMaxpage(2, condition);

		} catch (Exception e) {
			return "{" + '"' + "searchBlogMaxPage" + '"' + ":" + '"' + 0 + '"' + "}";
		}
		return "{" + '"' + "searchBlogMaxPage" + '"' + ":" + '"' + page + '"' + "}";
	}

	/**

	 * 按照条件搜索博客
	 * 
	 * @param condition
	 * 搜索条件
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/searchBlogs")
	public List<Blog> searchBlogs(@RequestParam(name = "condition") String condition,
			@RequestParam(name = "pageNo") Integer pageNo) {
		return this.getBlogService().searchBlogByCondition(condition, pageNo);
	}

	/**
	 * 获取点赞排行榜
	 * 
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/searchBlogByupvoteRank")
	public List<Blog> searchBlogByupvoteRank() {

		// 当天点赞榜
		return this.getBlogService().searchBlogByUpvoteRank();
	}

}